﻿using DataBaseConnect.IRepositories;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Utils;

namespace DataBaseConnect.Repositories
{
    public  class ButtonRepository:FonourRepositoryBase<Button>, IButtonRepository
    {

        public ButtonRepository(MyDataBase dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 新增按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public ReceiveStatus AddButtonStyleMsg(ButtonStyle Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<ButtonStyle>().Add(Input);
                _dbContext.SaveChanges();
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
           
        }

        /// <summary>
        /// 编辑按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public ReceiveStatus EditButtonStyleMsg(ButtonStyle Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<ButtonStyle>().Update(Input);
                _dbContext.SaveChanges();
                receiveStatus.msg = "更新成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据按钮样式id获取对应的按钮样式
        /// </summary>
        /// <param name="where">筛选条件</param>
        /// <returns></returns>
        public ButtonStyle GetButtonStyleIdMsg(Expression<Func<ButtonStyle, bool>> where)
        {
            return _dbContext.Set<ButtonStyle>().FirstOrDefault(where);
        }

        /// <summary>
        /// 获取所有按钮样式&&查询
        /// </summary>
        /// <param name="ButtonStyleName">按钮样式名称</param>
        /// <returns></returns>
        public List<ButtonStyle> GetButtonStyleList(string ButtonStyleName)
        {
            if (!string.IsNullOrEmpty(ButtonStyleName))
            {
                return _dbContext.Set<ButtonStyle>().Where(f => f.ButtonStyleName == ButtonStyleName).ToList();
            }
            else
            {
                return _dbContext.Set<ButtonStyle>().ToList();
            }
        }

        /// <summary>
        /// 根据条件查询按钮权限表
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public List<ButtonRole> GetButtonRoleByMenuIdList(Expression<Func<ButtonRole, bool>> where)
        {
            return _dbContext.Set<ButtonRole>().Where(where).ToList();
        }

        /// <summary>
        /// 设置角色菜单权限
        /// </summary>
        /// <param name="MenuRole">实体信息</param>
        /// <returns></returns>
        public ReceiveStatus SetButtonRoleMsg(List<ButtonRole> ButtonRole)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<ButtonRole>().AddRange(ButtonRole);
                _dbContext.SaveChanges();
                receiveStatus.msg = "设置成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据条件删除按钮权限表信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public ReceiveStatus DelButtonRoleMsg(Expression<Func<ButtonRole, bool>> where)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<ButtonRole>().Where(where).ToList().ForEach(it => _dbContext.Set<ButtonRole>().Remove(it));
                _dbContext.SaveChanges();
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }
    }
}
